<template>
  <div class="approval" ref="imageWrapper">
    <div class="process-engine" style="width: 100%; height: 2000px">
      <ht-approval
        ref="childrenRef"
        :process-instance-id="processInstanceId"
        :options="options"
        :custom="custom"
        @submit="afterSubmit"
        @reject="afterReject"
        @back="backAction"
        @cancelSubmit="afterCancelSubmit"
        :before-fill-opinion="saveNotifyOpinion"
        :before-submit="beforeSubmit"
        :before-reject="beforeReject"
        :show-task-history="true"
        :history-dialog="historyDialog"
        :task-load="taskLoad"
        :message-id="messageId"
        :show-upload-btn="false"
        :hidden-task-def-key="hiddenTaskDefKey"
        :snapshotConfigs="snapshotConfigs"
      >
      </ht-approval>
    </div>
    
  </div>
</template>
<script>
import Vue from 'vue'
import '@/config/upload.install.js'
import { getAction, getActionAsync, postAction } from '@/api/manage'
import { ACCESS_TOKEN, USER_NAME } from '@/store/mutation-types'
import { strNotNull, getParamValFromUrl } from '@/utils/common.js'
export default {
  components: {
    htApproval: () => import('@/config/ht-approval.umd.min.js'),
  },
  data() {
    return {
      loadProcess: false,
      loading: false,
      url: {
        auditProjectPass: '/rad/technology/auditProjectPass',
        auditProjectNotPass: '/rad/technology/auditProjectNotPass',
        auditProjectBack: '/rad/technology/auditProjectBack',
        auditProjectCancel: '/rad/technology/auditProjectCancel'
      },
      options: {
        request: {
          baseUrl: this.API_BASE_URL, // 业务系统根路径
          timeOut: 5000, // 请求超时时间
          interceptors: (config) => {
            config.headers['X-Access-Token'] = Vue.ls.get(ACCESS_TOKEN)
            return config
          },
        },
        userId: Vue.ls.get(USER_NAME),
      },
      taskType: 0, // 任务类型，默认0：待办任务，1：抄送任务，2：已办任务
      returnShowConfirm: false, // 退回显示确认框
      custom: {},
      snapshotConfigs: [
        {
          type: 'ref',
          value: 'imageWrapper'
        }
      ],
      historyDialog: {
        // 审批历史模态框属性
      },
      currentTask: {},
      hiddenTaskDefKey:"submit", //发起岗默认完成
    }
  },
  props:{
    id: {
      Type: String,
      default: '',
    },
    processInstanceId: {
      Type: String,
      default: '',
    },
    messageId: {
      Type: String,
      default: '',
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
    if(!strNotNull(this.id)){
      this.id = getParamValFromUrl('id')
    }
    if(!strNotNull(this.processInstanceId)){
      this.processInstanceId = getParamValFromUrl('processInstanceId')
    }
    if(!strNotNull(this.messageId)){
      this.messageId = getParamValFromUrl('messageId') 
    }
    this.loadProcess = true
  },
  computed: {},
  methods: {
    async auditSubmit(url,backKey) {
      var result = false;
      await getActionAsync(url, { id: this.id, backKey: backKey })
        .then((res) => {
          if (res.code == 200) {
            result = true
          } else {
            this.$message.warning('操作失败:' + res.message)
          }
        }).catch((res) => {
          this.$message.warning('操作异常:' + res.message)
        })
        return result;
    },
    //-------------流程回调------start 
    beforeSubmit(res) {
      return true;
    },
    afterSubmit(res) {
       this.$router.go(0)
      return this.auditSubmit(this.url.auditProjectPass,null)
    },
    beforeReject(res) {
      return true;
    },
    afterReject(res) {
       this.$router.go(0)
      // 用户点击驳回后成功返回后的处理
      return this.auditSubmit(this.url.auditProjectNotPass,null)
    },
    backAction(res) {
      this.$router.go(0)
      var backKey = this.$refs.childrenRef.currentNode.TASK_DEF_KEY_
      return this.auditSubmit(this.url.auditProjectBack,backKey)
    },
    //撤回后
    afterCancelSubmit() {
       this.$router.go(0)
      return this.auditSubmit(this.url.auditProjectCancel,null)
    },
    // 生成抄送任务方法，非必须
    sendNotify() {
      // 参数一：抄送人，参数二：工作台打开详情链接
      // this.$refs.childrenRef.saveBpmNotify('liangyh', 'http://xxxxx.com')
    },
    saveNotifyOpinion() {
      // 保存抄送意见前置动作
      debugger
      return true
    },
    taskLoad(task) {
      // 当前任务加载回调函数，获取当前任务信息
      this.currentTask = task
      var taskDefKey = this.currentTask.taskDefKey
      window.parent.postMessage(taskDefKey, "*");
    },
  },
  destroyed() {
    document.removeEventListener('scroll', this.handleScroll)
  },
}
</script>
<style lang="less" scoped>
.main {
  height: unset;
}
.approval {
  height: max-content;
  background: #f5f5f5;
  .process-engine {
    /deep/ #process-container {
      width: unset !important;
    }
    /deep/ #process-container .process-item .info-fullMessage {
      max-width: 1200px;
    }
  }
}
</style>